Ext.namespace('WebApp');

Ext.App = function(config) {
    //
    Ext.Direct.addProvider(WebApp.REMOTING_API);
    // set up StateProvider
    this.initStateProvider();

    // array of views
    this.views = [];


    Ext.Direct.on('exception', function(e) {
        Ext.Msg.alert('Direct Exception', e.message);
    });
    Ext.apply(this, config);
    if (!this.api.actions) { this.api.actions = {}; }

    // init when onReady fires.
    Ext.onReady(this.onReady, this);

    Ext.App.superclass.constructor.apply(this, arguments);
}
Ext.extend(Ext.App, Ext.util.Observable, {

 
    STATUS_EXCEPTION: 'exception',
    STATUS_VALIDATION_ERROR: "validation",
    STATUS_ERROR: "error",
    STATUS_NOTICE: "notice",
    STATUS_OK: "ok",
    STATUS_HELP: "help",

    msgCt: null,

   
    onReady: function() {
        
        this.msgCt = Ext.DomHelper.insertFirst(document.body, { id: 'msg-div' }, true);
        this.msgCt.setStyle('position', 'absolute');
        this.msgCt.setStyle('z-index', 9999);
        this.msgCt.setWidth(300);
    },

    initStateProvider: function() {
      
        var days = '';        // expires when browser closes
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var exptime = "; expires=" + date.toGMTString();
        } else {
            var exptime = null;
        }

        
        Ext.state.Manager.setProvider(new Ext.state.CookieProvider({
            path: '/',
            expires: exptime,
            domain: null,
            secure: false
        }));
    },

  
    registerView: function(view) {
        this.views.push(view);
    },

 
    getViews: function() {
        return this.views;
    },

  
    registerActions: function(actions) {
        Ext.apply(this.api.actions, actions);
    },

 
    getAPI: function() {
        return this.api;
    },

 
    setAlert: function(status, msg) {
        this.addMessage(status, msg);
    },

  
    addMessage: function(status, msg) {
        var delay = 3;    // <-- default delay of msg box is 1 second.
        if (status == false) {
            delay = 5;    // <-- when status is error, msg box delay is 3 seconds.
        }
      
        delay = msg.length / 13.3;
        if (delay < 3) {
            delay = 3;
        }
        else if (delay > 9) {
            delay = 9;
        }

        this.msgCt.alignTo(document, 't-t');
        Ext.DomHelper.append(this.msgCt, { html: this.buildMessageBox(status, String.format.apply(String, Array.prototype.slice.call(arguments, 1))) }, true).slideIn('t').pause(delay).ghost("t", { remove: true });
    },

  
    buildMessageBox: function(title, msg) {
        switch (title) {
            case true:
                title = this.STATUS_OK;
                break;
            case false:
                title = this.STATUS_ERROR;
                break;
        }
        return [
            '<div class="app-msg">',
            '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
            '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3 class="x-icon-text icon-status-' + title + '">', title, '</h3>', msg, '</div></div></div>',
            '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
            '</div>'
        ].join('');
    },

    decodeStatusIcon: function(status) {
        iconCls = '';
        switch (status) {
            case true:
            case this.STATUS_OK:
                iconCls = this.ICON_OK;
                break;
            case this.STATUS_NOTICE:
                iconCls = this.ICON_NOTICE;
                break;
            case false:
            case this.STATUS_ERROR:
                iconCls = this.ICON_ERROR;
                break;
            case this.STATUS_HELP:
                iconCls = this.ICON_HELP;
                break;
        }
        return iconCls;
    },

  
    setViewState: function(key, value) {
        Ext.state.Manager.set(key, value);
    },

 
    getViewState: function(key) {
        return Ext.state.Manager.get(key);
    },

 
    t: function(words) {
        return words;
    },

    handleResponse: function(res) {
        if (res.type == this.STATUS_EXCEPTION) {
            return this.handleException(res);
        }
        if (res.message.length > 0) {
            this.setAlert(res.status, res.message);
        }
    },

    handleException: function(res) {
        Ext.MessageBox.alert(res.type.toUpperCase(), res.message);
    }
});




WebApp.DataProxy = function(config) {
    WebApp.DataProxy.superclass.constructor.call(this, Ext.apply({
        api: {
            read: './Datapool.mvc/ReadDataObj',
            create: 'CreateCustomers',
            update: 'UpdateCustomers',
            destroy: 'DeleteCustomers'
        }
    }, config));
};
Ext.extend(WebApp.DataProxy, Ext.data.HttpProxy, {});


WebApp.DataStore = function(config) {
    WebApp.DataStore.superclass.constructor.call(this, Ext.apply({
        paramNames: {
            "start": "Skip",
            "limit": "Take",
            "sort": "Sort",
            "dir": "SortDir",
            "target": "target",
            "param": "params"
        },
        proxy: new WebApp.DataProxy(),
        //        reader: Customersreader,
        //        writer: Customerswriter,  // <-- plug a DataWriter into the store just as you would a Reader
        autoSave: true,
        listeners: {
            write: function(store, action, result, res, rs) {
                App.setAlert(res.success, res.message); // <-- show user-feedback for all write actions
            },
            exception: function(proxy, type, action, options, res, arg) {
                if (type === 'remote') {
                    Ext.Msg.show({
                        title: 'REMOTE EXCEPTION',
                        msg: res.message,
                        icon: Ext.MessageBox.ERROR,
                        buttons: Ext.Msg.OK
                    });
                }
            }
        },
        totalProperty: '',
        root: 'ListDataObj',
        remoteSort: true,
        autoLoad: false,
        restful: true,
        //id: "KD_CUSTOMER",
        method: "POST"
    }, config));
};

Ext.extend(WebApp.DataStore, Ext.data.JsonStore, {});



